package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.dto.MyHandleAlertDataDto;
import com.zzyl.nursing.dto.MyPageQueryAlertDataDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-25
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    private LoginUser loginUser;
    @Override
    public List<AlertData> selectAlertDataList(MyPageQueryAlertDataDto alertData, HttpServletRequest request)
    {

        //解析token 获取到当前登录人的id
         loginUser = tokenService.getLoginUser(request);
        //Long id = alertData.getId();
        //AlertData alertData1 = new AlertData();
        //新建一个查询对象
        LambdaQueryWrapper<AlertData> wrapper = new LambdaQueryWrapper<AlertData>();
        //如果搜索条件为空，则不设置条件,查询全部
        if(!StringUtils.isEmpty(alertData.getDeviceName())){
            wrapper.like(AlertData::getDeviceName,alertData.getDeviceName());
        }
        if(alertData.getEndTime()!=null && alertData.getStartTime()!=null){
            wrapper.between(AlertData::getCreateTime,alertData.getStartTime(),alertData.getEndTime());
        }
        wrapper.ne(AlertData::getUserId,1);
        //根据条件查询符合条件的集合
        List<AlertData> alertDataList = alertDataMapper.selectList(wrapper);

        //查询全部的报警数据集合
       // List<AlertData> alertDataList = alertDataMapper.selectAlertDataList(alertData1);
        //创建新集合用于接收登录人需要处理的报警数据
        List<AlertData> resultAlertDataList = new ArrayList<>();
        if(loginUser.getUserId()==1){
            return alertDataList;
        }
        alertDataList.forEach(alertDataItem -> {
            if(alertDataItem.getUserId().equals(loginUser.getUserId())){
                resultAlertDataList.add(alertDataItem);
            }
        });
        return resultAlertDataList;
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }

    /**
     * 处理报警数据
     * @param alertData 待处理的报警数据
     */

    @Override
    public void handleAlertData(MyHandleAlertDataDto alertData) {
        // 获取处理结果
        Long id = alertData.getId();
        String processingResult = alertData.getProcessingResult();
        LocalDateTime processingTime = alertData.getProcessingTime();
        //解析时间类
        /*LocalDateTime localDateTime = parseToLocalDateTime(String.valueOf(processingTime));*/
        // 查询目标数据
        AlertData alertDataTarget = alertDataMapper.selectAlertDataById(id);

        // 设置处理结果
        alertData.setProcessingResult(processingResult);
        alertData.setProcessingTime(processingTime);
        //设置状态为已处理
        alertDataTarget.setStatus(1);
        //设置处理人ID
        alertDataTarget.setUserId(loginUser.getUserId());
        alertDataTarget.setProcessorName(loginUser.getUsername());
        //设置处理结果
        String res = alertData.getProcessingResult();
        alertDataTarget.setProcessingResult(res);
        alertDataTarget.setProcessingTime(LocalDateTime.now());
        //保存处理结果
        alertDataMapper.updateAlertData(alertDataTarget);
    }
    private static LocalDateTime parseToLocalDateTime(String timeString) {
        // ISO 8601 格式的日期时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

        // 解析时间字符串
        return LocalDateTime.parse(timeString.replace("Z", "+00:00"), formatter);
    }
}
